package com.xh;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;

/**
 * description: HelloNettyServerInitializer <br>
 * date: 2021/11/13 20:31 <br>
 * author: xiehui <br>
 * version: 1.0 <br>
 * 初始化器，channel注册之后，会执行里面的相应的初始化方法
 */

public class HelloNettyServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        // 通过socketchannel去获取对应的管道
        ChannelPipeline pipeline = channel.pipeline();
        /**
         * 通过管道添加handler
         * HttpServerCodec：是由netty自己提供的助手类了，可以理解为拦截器
         * 当请求到服务器时，我们需要解码，响应到客户端做编码
         */
        pipeline.addLast("HttpServerCodec",new HttpServerCodec());
        // 添加自定义助手类，给客户端浏览器渲染hello netty
        pipeline.addLast("CustomHandler",new CustomHandler());

    }
}
